/*
 *  Class name: EntityDeepComparator.java
 *  Version: 1.0
 *  Date: 31.10.2013
 */
package cz.muni.fi.pa165.soccergamesarchive.persistencelayer.utils;

import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Player;
import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Team;
import java.util.Comparator;
import java.util.List;

import static java.util.Collections.sort;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Class make deep compare (equals) of entities (Team, Player, etc.)
 *
 * @author Michal Ďuriš
 */
public class EntityDeepComparator {

    private static final Comparator<Team> TEAM_ID_COMPARATOR = (Team o1, Team o2)
            -> o1.getId().compareTo(o2.getId());
    private static final Comparator<Player> PLAYER_ID_COMPARATOR = (Player o1,
                                                                    Player o2)
            -> o1.getId().compareTo(o2.getId());

    public EntityDeepComparator() {
    }

    public static void assertDeepEquals(Team expected, Team actual) {

        assertEquals(expected, actual);
        assertDeepPlayerListEquals(expected.getPlayers(), actual.getPlayers());
    }

    public static void assertDeepEquals(Player expected, Player actual) {

        assertEquals(expected, actual);
        assertEquals(expected.getFirstName(), actual.getFirstName());
        assertEquals(expected.getLastName(), actual.getLastName());
        assertEquals(expected.getShirtNumber(), actual.getShirtNumber());
        assertEquals(expected.getTeam(), actual.getTeam());
    }

    public static void assertDeepTeamListEquals(List<Team> expected,
                                                List<Team> actual) {

        assertNotNull(expected);
        assertNotNull(actual);

        assertEquals(expected.size(), actual.size());

        sort(expected, TEAM_ID_COMPARATOR);
        sort(actual, TEAM_ID_COMPARATOR);

        for (int i = 0; i < expected.size(); i++) {
            assertDeepEquals(expected.get(i), actual.get(i));
        }
    }

    public static void assertDeepPlayerListEquals(List<Player> expected,
                                                  List<Player> actual) {

        assertNotNull(expected);
        assertNotNull(actual);

        assertEquals(expected.size(), actual.size());

        sort(expected, PLAYER_ID_COMPARATOR);
        sort(actual, PLAYER_ID_COMPARATOR);

        for (int i = 0; i < expected.size(); i++) {
            assertDeepEquals(expected.get(i), actual.get(i));
        }
    }

}
